package com.jincheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jincheng.system.domain.Activity;
import com.jincheng.system.domain.Slideshow;
import com.jincheng.system.mapper.*;
import com.jincheng.system.service.IMappingService;
import com.jincheng.system.service.ISysDeptService;
import com.jincheng.system.service.ITribeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MappingServiceImpl implements IMappingService {

    private final ISysDeptService deptService;

    private final ITribeService tribeService;

    private final SlideshowMapper slideshowMapper;
    private final SysUserMapper sysUserMapper;
    private final ActivityMapper activityMapper;
    private final TribeMapper tribeMapper;
    private final SysDeptMapper sysDeptMapper;

    public MappingServiceImpl(ISysDeptService deptService,
                              ITribeService tribeService,
                              SlideshowMapper slideshowMapper, SysUserMapper sysUserMapper, ActivityMapper activityMapper, TribeMapper tribeMapper, SysDeptMapper sysDeptMapper) {
        this.deptService = deptService;
        this.tribeService = tribeService;
        this.slideshowMapper = slideshowMapper;
        this.sysUserMapper = sysUserMapper;
        this.activityMapper = activityMapper;
        this.tribeMapper = tribeMapper;
        this.sysDeptMapper = sysDeptMapper;
    }

    /**
     * 获取筛选数据
     *
     * @return 筛选数据
     */
    @Override
    public Map<String, Object> getActivityScreens() {
        Map<String, Object> data = new HashMap<>();
        Map<String, Object> options = new HashMap<>();

        // 院系查询列表
        options.put("title", "归属院系");
        options.put("type", "deptId");
        options.put("options", deptService.selectDeptStairOptions());
        data.put("dept", new HashMap<>(options));
        options.clear();

        // 部落查询列表
        options.put("title", "归属部落");
        options.put("type", "tribeId");
        options.put("options", tribeService.selectTribeOptionsByUserId());
        data.put("tribe", new HashMap<>(options));
        options.clear();

        // 组织查询列表
        options.put("title", "归属组织");
        options.put("type", "orgId");
        options.put("options", tribeService.selectOrgOptions());
        data.put("org", options);
        return data;
    }

    @Override
    public Map<String, Object> getTribeScreens() {
        return Map.of();
    }

    /**
     * 获取首页轮播图
     *
     * @return 轮播图
     */
    @Override
    public List<String> getSlideshow() {
        LambdaQueryWrapper<Slideshow> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Slideshow::getUpdateTime)
                .last("limit 5");
        List<Slideshow> slideshowList = slideshowMapper.selectList(wrapper);

        return slideshowList.stream().map(Slideshow::getUrl).toList();
    }

    /**
     * 获取系统统计信息
     *
     * @return 系统统计信息
     */
    @Override
    public Map<String, Object> getSystemStatistics() {
        Map<String, Long> stringObjectMap = sysUserMapper.selectCountOrderBySex();

        Long maleCount = stringObjectMap.get("male");
        Long femaleCount = stringObjectMap.get("female");
        Long userCount = stringObjectMap.get("total");

        Long activityCount = activityMapper.selectCount(new QueryWrapper<>());
        Long tribeCount = tribeMapper.selectCount(new QueryWrapper<>());
        Long deptCount = sysDeptMapper.selectCount();

        return Map.of(
                "userCount", userCount,
                "maleCount", maleCount,
                "femaleCount", femaleCount,
                "activityCount", activityCount,
                "tribeCount", tribeCount,
                "deptCount", deptCount
        );
    }
}
